package it.appcatalogobase;

import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class LoadTask extends AsyncTask<Void, Void, Void> {

	private ProgressDialog progressDialog;
	private Activity a;
	private String type;
	private ProdottoDataSource datasourcepr;
	private GridView gridView;
	private String[] names;
	private String[] ids;
	private String[] images;
	private String[] descr;
	private String[] colori;
	private List<Prodotto> valuesPr;

	public LoadTask(Activity a, String type, GridView gridView) {
		this.a = a;
		this.progressDialog = new ProgressDialog(a);
		this.type = type;
		this.gridView = gridView;
		datasourcepr = new ProdottoDataSource(a);
		datasourcepr.open();
	}

	protected void onPreExecute() {
		a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
		progressDialog.setMessage("...Carico i dati...");
		progressDialog.setTitle("Caricamento");
		progressDialog.show();
		progressDialog.setOnCancelListener(new OnCancelListener() {
			public void onCancel(DialogInterface arg0) {
				LoadTask.this.cancel(true);
			}
		});
	}

	@Override
	protected Void doInBackground(Void... arg0) {
		if (type.equals("all")) {

			// datasourcepr.createProdotto("Caffè amaro", "amaro amarissimo",
			// "Caffè", "caffe_amaro");
			// datasourcepr.createProdotto("Caffè dolce", "dolce dolcissimo",
			// "Caffè", "caffe_dolce");
			// datasourcepr.createProdotto("Birra", "boooooooo", "altro",
			// "birra");

			valuesPr = datasourcepr.getAllProdotti();
			Iterator<Prodotto> it = valuesPr.iterator();
			names = new String[valuesPr.size()];
			ids = new String[valuesPr.size()];
			images = new String[valuesPr.size()];
			descr = new String[valuesPr.size()];
			colori = new String[valuesPr.size()];
			int counter = 0;
			while (it.hasNext()) {
				Prodotto p = it.next();
				names[counter] = p.getNome();
				ids[counter] = String.valueOf(p.getId());
				images[counter] = p.getImmagine();
				descr[counter] = p.getDescrizione();
				colori[counter] = p.getColori();
				counter++;
			}
			if (counter == 0) {
				Toast t = Toast.makeText(a, "NESSUN PRODOTTO PRESENTE NEL CATALOGO", Toast.LENGTH_LONG);
				t.setGravity(Gravity.CENTER, 0, 0);
				t.show();
			}
		} else if (type.equals("cat")) {
			String cat = a.getIntent().getStringExtra("cat");
			Long id = a.getIntent().getLongExtra("id", 1);
			valuesPr = datasourcepr.getAllProdottiFromCat(cat);
			Iterator<Prodotto> it = valuesPr.iterator();
			names = new String[valuesPr.size()];
			ids = new String[valuesPr.size()];
			images = new String[valuesPr.size()];
			descr = new String[valuesPr.size()];
			colori = new String[valuesPr.size()];
			int counter = 0;
			while (it.hasNext()) {
				Prodotto p = it.next();
				names[counter] = p.getNome();
				ids[counter] = String.valueOf(p.getId());
				images[counter] = p.getImmagine();
				descr[counter] = p.getDescrizione();
				colori[counter] = p.getColori();
				counter++;
			}
			if (counter == 0) {
				Toast t = Toast.makeText(a, "NESSUN PRODOTTO PRESENTE PER LA CATEGORIA SELEZIONATA", Toast.LENGTH_LONG);
				t.setGravity(Gravity.CENTER, 0, 0);
				t.show();
			}
		}
		// TODO Auto-generated method stub
		return null;
	}

	protected void onPostExecute(Void v) {
		gridView.setAdapter(new ImageAdapter(a, names, ids, images));
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				Intent i = new Intent(a, ProductDettaglioActivity.class);
				i.putExtra("id", ids[position]);
				i.putExtra("name", names[position]);
				i.putExtra("des", descr[position]);
				i.putExtra("imm", images[position]);
				if (valuesPr.size() > 1) {
					if (type.equals("cat"))
						i.putExtra("type", "cat");
					else
						i.putExtra("type", "all");
				} else
					i.putExtra("type", "single");
				i.putExtra("colori", colori[position]);
				a.startActivity(i);
				// i.putExtra("id", ((TextView)
				// v.findViewById(R.id.grid_item_id)).getText().toString());
			}
		});
//		try {
			if ((this.progressDialog != null) && this.progressDialog.isShowing()) {
				this.progressDialog.dismiss();
			}
//		} catch (final IllegalArgumentException e) {
//			// Handle or log or ignore
//		} catch (final Exception e) {
//			// Handle or log or ignore
//		} finally {
//			this.progressDialog = null;
//		}  
		a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
	}

}
